﻿namespace OboutInc.SlideMenu
{
    using System;
    using System.Collections;
    using System.Reflection;

    public class SlideMenuItems : CollectionBase
    {
        public void Add(SlideMenuItem i)
        {
            base.List.Add(i);
        }

        public SlideMenuItem GetItemById(string itemId)
        {
            foreach (SlideMenuItem item in base.List)
            {
                if (item.ID == itemId)
                {
                    return item;
                }
            }
            return null;
        }

        public void InsertItemAt(string itemId, SlideMenuItem item)
        {
            int index = 0;
            SlideMenuItem itemById = this.GetItemById(itemId);
            if (itemById == null)
            {
                throw new Exception("Slide menu item with id \"" + itemId + "\" not found.");
            }
            if ((item is Child) && (itemById is Parent))
            {
                index = base.List.IndexOf(itemById) + 1;
                while ((index < base.List.Count) && (base.List[index] is Child))
                {
                    index++;
                }
                base.List.Insert(index, item);
            }
            else
            {
                base.List.Insert(base.List.IndexOf(itemById) + 1, item);
            }
        }

        public void Remove(int i)
        {
            base.List.RemoveAt(i);
        }

        public SlideMenuItem this[int index]
        {
            get
            {
                return (SlideMenuItem) base.List[index];
            }
        }
    }
}

